package com.ccl.iot.device;

import com.ccl.iot.privates.BitmapCollector;
import com.ccl.iot.privates.BitmapMask;
import com.ccl.iot.privates.ForbidMask;
import com.ccl.iot.publics.UIUtils;
import com.ccl.iot.privates.WarningMask;
import com.ccl.iot.App;
import com.ccl.iot.IOTNetAPIEnum;
import com.ccl.iot.R;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Bitmap.Config;

public class MultiFunc5525IconProvider extends DeviceIconProvider {
	
	public static boolean RegisterProviderType(){
		return RegisterProviderType(MultiFunc5525IconProvider.class);
	}
	
	//=========================================modify start=====================================================
		public synchronized static Bitmap Build(int aStyle, int aWidth, int aHeight, int aHighLightColor, int aBackColor, boolean isOn, boolean isPressed, BitmapMask aMask, int iconId){
			if (aWidth > 0 && aHeight > 0){
				Bitmap tIcon;
				String tKey = "MultiFunc." + aStyle + "." + aWidth + "x" + aHeight + ":" + aHighLightColor + "-" + aBackColor + (aMask == null ? "" : "+" + aMask.getHashCode())+ "."+iconId;
				
				tIcon = BitmapCollector.Get(tKey);
				
				if (tIcon == null){
					tIcon = Bitmap.createBitmap(aWidth, aHeight, aBackColor != Color.TRANSPARENT ? Config.RGB_565 : Config.ARGB_8888);
					
					Canvas tCanvas = new Canvas(tIcon);
					
					Bitmap centerIcon = null;
					
					// TODO 设置意向控制多功能转换器(5525)的图标

						/*if(isOn){
							centerIcon = BitmapFactory.decodeResource(App.GetInstance().getResources(), DeviceIconProvider.ICON_LIST_SWITCH_ON[iconId]);
						}else{
							centerIcon = BitmapFactory.decodeResource(App.GetInstance().getResources(), DeviceIconProvider.ICON_LIST_SWITCH_OFF[iconId]);
						}*/
						
						centerIcon = BitmapFactory.decodeResource(App.GetInstance().getResources(), R.drawable.icon_unknown);

					
					int size = aWidth > aHeight ? aHeight : aWidth;
					
					centerIcon = UIUtils.scaleBitmap(centerIcon, size, size);
					
					float left = (aWidth-centerIcon.getWidth())/2;
					float top = (aHeight-centerIcon.getHeight())/2;
					
					
					if (aBackColor != Color.TRANSPARENT){
						tCanvas.drawColor(aBackColor);
					}
					
					tCanvas.drawBitmap(centerIcon, left, top, new Paint());
					
					if (aMask != null){
						aMask.paintMask(tCanvas);
					}
					
					tCanvas.save();
					tCanvas.restore();
					
					BitmapCollector.Add(tKey, tIcon);
					
					return tIcon;
				}
				
				return tIcon;
			}
			
			return null;
		}
		
		//=========================================modify end=====================================================
		
		public static int GetHighLigthColor(boolean aOnline, boolean aIsOn, boolean aPressing){
			int tColor = Color.DKGRAY;
			
			if (aOnline){
				if (aIsOn){
					if (aPressing){
						tColor = Color.rgb(100, 200, 100);
					}else{
						tColor = Color.GREEN;
					}
				}else{
					if (aPressing){
						tColor = Color.rgb(200, 100, 100);
					}else{
						tColor = Color.RED;
					}
				}
			}
			
			return tColor;
		}
		
		public static int GetHighLigthColor(IOTMultiFunc5525 aDevice, boolean aPressing){
			if (aDevice != null){
				return GetHighLigthColor(aDevice.isOnline(), aDevice.hasPatch(), aPressing);
			}
			
			return Color.TRANSPARENT;
		}
		
		public static Bitmap Build(int aStyle, int aWidth, int aHeight, int aBackColor, boolean aOnline, boolean aIsOn, boolean aPressing, int aAuth, int iconId){
			Bitmap tIcon = null;
			int tColor = GetHighLigthColor(aOnline, aIsOn, aPressing);
			
//			if (aOnline){
//				BitmapMask tMask = null;
//
//				if (aAuth == IOTNetAPIEnum.EAUTH_STATE.EAUTH_STATE_UNKNOWN ||
//						aAuth == IOTNetAPIEnum.EAUTH_STATE.EAUTH_STATE_NONE ||
//						aAuth == IOTNetAPIEnum.EAUTH_STATE.EAUTH_STATE_NONE ||
//						aAuth == IOTNetAPIEnum.EAUTH_STATE.EAUTH_STATE_WAITTING ||
//						aAuth == IOTNetAPIEnum.EAUTH_STATE.EAUTH_STATE_REJECT ||
//						aAuth == IOTNetAPIEnum.EAUTH_STATE.EAUTH_STATE_QUIT){
//					aIsOn = false;
//					aPressing = false;
//					tMask = new ForbidMask(new RectF(aWidth * 0.7f, aHeight * 0.7f, aWidth, aHeight), Color.WHITE);
//				}else if (aAuth == IOTNetAPIEnum.EAUTH_STATE.EAUTH_STATE_READ){
//					aPressing = false;
//					tMask = new WarningMask(new RectF(aWidth * 0.7f, aHeight * 0.7f, aWidth, aHeight), Color.WHITE);
//				}
//
//				tIcon = Build(aStyle, aWidth, aHeight, tColor, aBackColor, aIsOn, aPressing, tMask, iconId);
//			}else{
//				tIcon = Build(aStyle, aWidth, aHeight, tColor, aBackColor, aIsOn, aPressing, null, iconId);
//			}

			tIcon = Build(aStyle, aWidth, aHeight, tColor, aBackColor, aIsOn, aPressing, null, iconId);
			
			return tIcon;
		}
		
		public static Bitmap Build(IOTMultiFunc5525 aDevice, int aStyle, int aWidth, int aHeight, int aBackColor, boolean aPressing){
			if (aDevice != null){
				return Build(aStyle, aWidth, aHeight, aBackColor, aDevice.isOnline(), false, aPressing, aDevice.getAuth(), aDevice.getSplitIconId());
			}
			
			return null;
		}

		
	/**
	 * No static function
	 */
		
	public MultiFunc5525IconProvider(IOTMultiFunc5525 aDevice) {
		super(aDevice);
	}

	@Override
	public Bitmap build(int aStyle, int aWidth, int aHeight, int aBackColor, boolean aPressing) {
		return Build((IOTMultiFunc5525)iDevice, aStyle, aWidth, aHeight, aBackColor, aPressing);
	}

}
